package com.ds.infrastructure.hyperspace.console.model.request.containerconfig.configdata;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ds.infrastructure.hyperspace.console.dto.configdata.EndpointsDTO;
import com.ds.infrastructure.hyperspace.console.dto.configdata.NodesDTO;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: yaozhendong
 * @create: 2019-09-20 16:40
 **/
@Data
@NoArgsConstructor
public class EndpointsForm {
    @JsonIgnore
    private String namespace;
    @ApiModelProperty(value = "节点类型:TARGET;ENTRY", example = "TARGET")
    private String type;
    @NotEmpty
    private List<@Valid TargetEndpointForm> nodes;

    public static EndpointsDTO toDTO(EndpointsForm item, String namespace, String type) {
        EndpointsDTO endpointsDTO = new EndpointsDTO();
        endpointsDTO.setNamespace(namespace);
        endpointsDTO.setType(type);
        List<NodesDTO> nodesDTOList = new ArrayList<>();
        item.getNodes().forEach(m -> nodesDTOList.add(TargetEndpointForm.toDTO(m)));
        endpointsDTO.setNodes(nodesDTOList);
        return endpointsDTO;
    }

    public static EndpointsForm toVO(EndpointsDTO dto) {
        EndpointsForm endpointsFrom = new EndpointsForm();
        endpointsFrom.setNamespace(dto.getNamespace());
        endpointsFrom.setType(dto.getType());
        List<TargetEndpointForm> nodesFromList = new ArrayList<>();
        dto.getNodes().forEach(m -> nodesFromList.add(TargetEndpointForm.toVO(m)));
        endpointsFrom.setNodes(nodesFromList);
        return endpointsFrom;

    }
}
